<?php
	class Product{

		function __construct(){
		}

		function __destruct(){
		}
		
		
		
		public function getProductAttributes($id=NULL,$active=NULL){
			$where = " WHERE pa.status != '2' ";
			if($id)$where.=" AND pa.id = ".$id;
			if($active)$where.=" AND pa.status = '1' ";
			$qry = "SELECT pa.* FROM category_attributes as pa ".$where;
			
			$rs = MyDB::$db->Execute($qry);
			$attributes = array();
			while ($row = $rs->fetchRow()) {
				$attributes[] = $row;
			}
			return $attributes;
		}
		
		public function getProducts(){
			$qry = "SELECT p.*, c.name as catname, sc.name as subcatname, ssc.name as subsubcatname 
					FROM product as p
					LEFT JOIN category as c ON c.id = p.product_category
					LEFT JOIN category as sc ON sc.id = p.product_subcategory
					LEFT JOIN category as ssc ON ssc.id = p.product_subsubcategory
					WHERE p.status != '2'
					ORDER BY p.modified DESC";
			$rs = MyDB::$db->Execute($qry);
			$products = array();
			while ($row = $rs->fetchRow()) {
				$products[] = $row;
			}
			return $products;
		}
		
		public function getProductById($id=''){
			$qry = "SELECT p.* FROM product as p
					WHERE p.status != '2' AND id = ".$id;
			$rs = MyDB::$db->Execute($qry);
			$products = array();
			while ($row = $rs->fetchRow()) {
				$products[] = $row;
			}
			return $products;
		}
		
		public function getActiveProducts($id=NULL){
			$status = 1;
			$qry = "SELECT p.id, p.product_name as name,p.attributes  FROM product as p
					WHERE p.status = ".MyDB::$db->qstr($status);
			if( !empty($id) ){
                $qry .= ' and id='.MyDB::$db->qstr($id);
            }
            $rs = MyDB::$db->Execute($qry);
			$products = array();
			while ($row = $rs->fetchRow()) {
				$products[] = $row;
			}
			return $products;
		}
        
        public function getProductAttributesNew($id=NULL){
            if($id)$where.=" AND vpa.product_id=".$id;
            $qry = "SELECT vpa.*, am.title
                    FROM product_attributes vpa, attribute_master am where vpa.attribute_id=am.id  ".$where;
            $rs = MyDB::$db->Execute($qry);
            $product_attributes = array();
            while ($row = $rs->fetchRow()) {
                $rowUnit = CommonUtility::getDataById($row['unit_id'], 'unit_master');
                if( $rowUnit ){
                    $row['unit'] = $rowUnit['title'];
                }
                $row['attribute_name'] = $row['title'];
                $product_attributes[] = $row;
            }
            return $product_attributes;
        }
		
	}	
?>